<?php
//error_reporting(0);

/**
 * 活动推荐管理 
 * 
 * @copyright caijian
 * @since 2012-03-21
 */


include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");


$g_cgival = array();
$g_pro = array();
$g_show = array();

function show_pro()
{
	global $g_show, $g_cgival, $g_pro, $config;
	$smarty = p_get_smarty ();
	$smarty->assign ( $g_show );
	if ( $g_cgival['mod'] == 'add' || $g_cgival['mod'] == 'edit' ) {
		$smarty->display ( "foodv2/event/event_tuijian_edit.html" );
	} else {
		$smarty->display ( "foodv2/event/event_tuijian.html" );
	}
}


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	cgi::both($g_cgival["tab"], "tab", 1);
	cgi::both($g_cgival["s_key"], "s_key", '');

	$g_cgival["s_key"] = string::un_script_code($g_cgival["s_key"]);
	$g_cgival["s_key"] = string::un_html($g_cgival["s_key"]);

	cgi::both($g_cgival['mod'],'mod','');
	cgi::both($g_cgival["id"], "id", 0);

	// edit by zhangchao 2012-05-29 加入分页
	cgi::both($g_cgival["p"], "p", 1);
	$g_cgival["p"] = intval($g_cgival["p"]);
	if($g_cgival["p"] <= 0)
	{
		$g_cgival["p"] = 1;
	}
	
	cgi::both($g_cgival["pc"], "pc", 40);
	$g_cgival["pc"] = intval($g_cgival["pc"]);
	if($g_cgival["pc"] <= 0)
	{
		$g_cgival["pc"] = 40;
	}
	// edit end
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	global $config;
	
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();

	$db_event = new db_food_event();

	$g_show['tab'] = intval($g_cgival['tab']);

	if ($g_cgival['mod'] == 'edit') {

		$g_show['data'] = $db_event->admin_get_food_event_by_id($g_cgival['id']);

	} elseif (! $g_cgival['mod']) {
		if (! $g_show['tab']) $g_show['tab'] = 1;

		$g_show['event_positions'] = $config['food_event_tj_pos'];
		$g_show['total_tj_num'] = $config['food_event_total_tj_num'][$g_show['tab']];

		$g_show['data'] = $db_event->admin_get_food_event_list($site_id,$g_show['tab'],$g_cgival["p"], $g_cgival["pc"], $g_cgival["s_key"]);	// WARRNG BY DONGPING  // edit by zhangchao 2012-05-29 加入分页
		foreach ($g_show['data'] as &$d) {
			$d['position'] = $config['food_event_tj_pos'][$d['event_tj_pos']];
		}
		$g_show['current_tj_num'] = $db_event->admin_get_food_event_tj_count($site_id,$g_show['tab']);
		
		// add by zhangchao 2012-05-29 增加分页
		$g_show["count"] = $db_event->admin_get_food_event_count($site_id, $g_show['tab'], $g_cgival["s_key"]);
		$max_page = page_bar::get_page_max($g_show["count"], $g_cgival["pc"], $g_cgival["p"]);
		$page_bar_style = array(
				"curt"		=> $g_cgival["p"],
				"max"		=> $max_page,
				"key"		=> "p",
		);
		$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$max_page}页&nbsp;" . page_bar::bar3($page_bar_style);
		$g_show['s_key'] = $g_cgival["s_key"];
		// add end
	}

}

try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/user/user_event.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


